package com.jh3502.helper.function.person;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jh3502.helper.R;

import java.util.ArrayList;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author harisucici
 */
public class GridImageAddAdapter extends RecyclerView.Adapter<GridImageAddAdapter.ViewHolder> {
    public final int TYPE_CAMERA = 1;
    public final int TYPE_PICTURE = 2;
    protected OnItemClickListener mItemClickListener;
    int addImg = R.drawable.ic_add_black_24dp;

    private LayoutInflater mInflater;
    private Context mContext;
    private ArrayList<Map<String, Object>> list = new ArrayList<>();
    private int selectMax = 9;
    /**
     * 点击添加图片跳转
     */
    private onAddPicClickListener mOnAddPicClickListener;

    public GridImageAddAdapter(Context context, onAddPicClickListener mOnAddPicClickListener) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.mOnAddPicClickListener = mOnAddPicClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public void setSelectMax(int selectMax) {
        this.selectMax = selectMax;
    }

    public void setList(ArrayList<Map<String, Object>> list) {
        this.list = list;
    }

    @Override
    public int getItemCount() {
        if (list.size() < selectMax) {
            return list.size() + 1;
        } else {
            return list.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isShowAddItem(position)) {
            return TYPE_CAMERA;
        } else {
            return TYPE_PICTURE;
        }
    }

    /**
     * 创建ViewHolder
     */
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.gv_filter_image, viewGroup, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        //itemView 的点击事件
        if (mItemClickListener != null) {
            viewHolder.itemView.setOnClickListener(v -> mItemClickListener.onItemClick(viewHolder.getAdapterPosition(), v));
        }
        return viewHolder;
    }

    private boolean isShowAddItem(int position) {
        int size = list.size();
        return position == size;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int position) {
        //少于8张，显示继续添加的图标
        if (getItemViewType(position) == TYPE_CAMERA) {
            viewHolder.fiv.setImageResource(addImg);
            viewHolder.fiv.setOnClickListener(v -> mOnAddPicClickListener.onAddPicClick(0, viewHolder.getAdapterPosition()));
            viewHolder.llDel.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.llDel.setVisibility(View.VISIBLE);
            viewHolder.llDel.setOnClickListener(view -> {
//                    mOnAddPicClickListener.onAddPicClick(1, viewHolder.getAdapterPosition());
                mOnAddPicClickListener.onAddPicClick(1, viewHolder.getAdapterPosition());
            });
            String path = (String) list.get(position).get("path");
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.color.black_text)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);

            Glide.with(mContext)
                    .asBitmap()
                    .load(path)
                    .apply(options)
                    .into(viewHolder.fiv);

        }
    }

    public void setAddImg(int addImg) {
        this.addImg = addImg;
    }

    public interface onAddPicClickListener {
        void onAddPicClick(int type, int position);
    }

    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.fiv)
        ImageView fiv;
        @BindView(R.id.ll_del)
        LinearLayout llDel;

        public ViewHolder(@NonNull View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
